<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';
    public $WX_APPID = 'wx6f8f0af9f1aa443a';

    public $WX_APPSECRET = 'f5a3d356ced0a028c91260222500d088';
    public $openid = '';
    public function _initialize()
    {   
       if(!session('openid')){
            $this->get_code();
       }else{
            if(request()->action()!='meet' && request()->action()!='mate'){
                //$this->mate();
            }
       }
    }

    public function index()
    {   
        return $this->view->fetch();
    }

    public function man(){
        return $this->view->fetch();
    }

    /**
     * 详情
     * @return [type] [description]
     */
    public function detail(){
        $id = input('param.id');
        $this->assign('id',$id);
        $this->view->id = $id;
        return $this->view->fetch();
    }


    /**
     * 详情
     * @return [type] [description]
     */
    public function mate_detail(){
        $id = input('param.id');
        $this->assign('id',$id);
        $this->view->id = $id;
        return $this->view->fetch();
    }

    /**
     * 个人信息
     * @return [type] [description]
     */
    public function info(){
        return $this->view->fetch();
    }

    /**
     * 个人中心页面
     * @return [type] [description]
     */
    public function my(){
        return $this->view->fetch();
    }

    /**
     * 查看用户列表
     * @return [type] [description]
     */
    public function look_list(){
        return $this->view->fetch();
    }

    /**
     * 查看用户列表
     * @return [type] [description]
     */
    public function look_lista(){
        return $this->view->fetch();
    }

    /**
     * 实名认证
     * @return [type] [description]
     */
    public function authen(){
        return $this->view->fetch();
    }

    /**
     * 实名认证提交页面
     * @return [type] [description]
     */
    public function authen_info(){
        return $this->view->fetch();
    }

    /**
     * 设置页面
     * @return [type] [description]
     */
    public function setting(){
        return $this->view->fetch();
    }

    /**
     * 单页页面
     * @return [type] [description]
     */
    public function page(){
        return $this->view->fetch();
    }


    /**
     * 单页页面
     * @return [type] [description]
     */
    public function page1(){
        return $this->view->fetch();
    }


    /**
     * 单页页面
     * @return [type] [description]
     */
    public function page2(){
        return $this->view->fetch();
    }



    /**
     * 单页页面
     * @return [type] [description]
     */
    public function page3(){
        return $this->view->fetch();
    }

    /**
     * 单页页面
     * @return [type] [description]
     */
    public function search_list(){
        $gender = input('param.gender');
        $age = input('param.age');
        $height = input('param.height');
        $income_id = input('param.income_id');
        $education_id = input('param.education_id');
        $birth_city = input('param.birth_city');
       // dump($gender);dump($age); dump($height); dump($income_id); dump($education_id);dump($birth_city);exit;
        $data=[
            'gender'=>$gender,
            'age'=>$age,
            'height'=>$height,
            'income_id'=>$income_id,
            'education_id'=>$education_id,
            'birth_city'=>$birth_city,
        ];
        $this->assign($data);
        return $this->view->fetch();
    }

    /**
     * 服务页面
     * @return [type] [description]
     */
    public function service(){
        return $this->view->fetch();
    }

    /**
     * 支付页面
     * @return [type] [description]
     */
    public function apply(){
        return $this->view->fetch();
    }

    public function weixin(){
        $id = input('param.id');
        return $this->view->fetch();
    }

    public function meet(){
        return $this->view->fetch();
    }

    public function mate(){
        return $this->view->fetch();
    }
    /**
     * [news description]
     * @return [type] [description]
     */
    public function tags(){
        return $this->view->fetch();
        // dump(57678);exit;
    }

    public function flower($value='')
    {
        return $this->view->fetch();
    }

    /**
     * 查看用户列表
     * @return [type] [description]
     */
    public function search(){
        return $this->view->fetch();
    }

    public function news()
    {
        $newslist = [];
        return jsonp(['newslist' => $newslist, 'new' => count($newslist), 'url' => 'https://www.fastadmin.net?ref=news']);
    }

    public function up(){
        return $this->view->fetch();
    }

    //PHP stdClass Object转array  
    public function object_array($array) {
        if(is_object($array)) { 
            $array = (array)$array;  
        }
        if(is_array($array)) {
             foreach($array as $key=>$value) {  
                 $array[$key] =$this->object_array($value);  
             }
         }
        return $array;
    }

    public function get_code(){
        $REDIRECT_URI = "http://".$_SERVER['SERVER_NAME']."/index/index/getLoginAccessToken";
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->WX_APPID."&redirect_uri=".urlencode($REDIRECT_URI)."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        header("Location:".$url);
    }

    //网页授权获取access_token
    public function getLoginAccessToken(){
        $code = input('param.code');
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->WX_APPID."&secret=".$this->WX_APPSECRET."&code=".$code."&grant_type=authorization_code";
        $geturl = geturl($url);
        $this->openid = $geturl['openid'];
        session('openid',$geturl['openid']);
        session('access_token',$geturl['access_token']);
        $info = $this->getLoginuserinfo($geturl['access_token'],$geturl['openid']);
        session('info',json_encode($info));
        $Location = "http://".$_SERVER['SERVER_NAME']."/index/index/meet";
        if(request()->action()!='meet' && request()->action()!='mate'){
            header("Location:".$Location);
        }

    }

    public function send($openid){
        $token = $this->get_accesstoken();
        //dump('token:'.$token);exit;
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$token}";
        $post_data['touser'] = $openid;
        $post_data['template_id'] = "T-iul539F8FUDiaaULKefcXNVqMWOjtn2ZqkIKIzGro";
        $post_data['data'] = array(
            "first"=>array(
              "value"=>"匹配对象通知"
            ),
            "keyword1"=>array(
              "value"=>'匹配对象'
            ),
            "keyword2"=>array(
              "value"=>'开始匹配'
            ),
            "keyword3"=>array(
              "value"=>date('Y-m-d H:i:s',time())
            ),
            "remark"=>array(
              "value"=>"系统已开始寻找属于你的那个人"
            )
        );
        $res = posturl($url,json_encode($post_data));
        return $res;
    }
    
    public function get_accesstoken(){
        $access_token_info = json_decode(file_get_contents('access_token.json'),true);
        $expires_time = time()+3600;
        if($access_token_info){
            if($access_token_info['expires_time']<time()){
                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->WX_APPID."&secret=".$this->WX_APPSECRET;
                $geturl = geturl($url);
                file_put_contents('access_token.json', '{"access_token": "'.$geturl['access_token'].'","expires_time": '.$expires_time.'}');
                return $geturl['access_token'];
            }else{
                return $access_token_info['access_token'];
            }
        }else{
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->WX_APPID."&secret=".$this->WX_APPSECRET;
            $geturl = geturl($url);
            file_put_contents('access_token.json', '{"access_token": "'.$geturl['access_token'].'","expires_time": '.$expires_time.'}');
            return $geturl['access_token'];
        }
        
    }

    // +----------------------------------------------------------------------
    // |        获取用户登录信息
    // +----------------------------------------------------------------------
    public function getLoginuserinfo($access_token,$openid)
    { 
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."";
        $geturl = geturl($url);
        return $geturl;
    }


}
